package com.cskaoyan.service;

import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.User;
import com.cskaoyan.bean.UserExample;
import com.cskaoyan.bean.user.*;
import com.cskaoyan.bean.wx_search.RegisterBo;
import com.cskaoyan.bean.wx_search.ResetBo;
import com.cskaoyan.bean.zhao_order_bean.BasePageData;
import com.cskaoyan.mapper.*;
import com.cskaoyan.utils.Md5Utils;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    AddressMapper addressMapper;
    @Autowired
    CollectMapper collectMapper;
    @Autowired
    FootprintMapper footprintMapper;
    @Autowired
    SearchHistoryMapper searchHistoryMapper;
    @Autowired
    FeedbackMapper feedbackMapper;

    @Override
    public BasePageData queryUser(String username, String mobile, BaseParam param) {
        //配置分页信息
        PageHelper.startPage(param.getPage(), param.getLimit());

        UserExample example = new UserExample();
        //构造条件
        UserExample.Criteria criteria = example.createCriteria();
        if (username != null && !"".equals(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (mobile != null && !"".equals(mobile)) {
            criteria.andMobileLike("%" + mobile + "%");
            //criteria.andMobileEqualTo(mobile);
        }
        //.andPasswordBetween("niupi","buniupi");
        //设置排序
        example.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<User> users = userMapper.selectByExample(example);
        //分页插件 → 做分页
        //执行完查询之后，可以获得分页结果 → 用哪些数据取决于你的前端需要哪些数据
        //PageInfo中需要传入查询结果
        return BasePageData.ok(users);
    }

    @Override
    public BasePageData queryAddress(Integer userId, String name, BaseParam param) {
        PageHelper.startPage(param.getPage(), param.getLimit());
        AddressExample example = new AddressExample();
        AddressExample.Criteria criteria = example.createCriteria();

        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        example.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<Address> addresses = addressMapper.selectByExample(example);
        return BasePageData.ok(addresses);
    }

    @Override
    public User queryUserDetail(Integer id) {
        User user = userMapper.selectByPrimaryKey(id);
        return user;
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateByPrimaryKey(user);
    }

    @Override
    public BasePageData queryCollect(Integer userId, Integer valueId, BaseParam param) {
        PageHelper.startPage(param.getPage(), param.getLimit());
        CollectExample example = new CollectExample();
        CollectExample.Criteria criteria = example.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null) {
            criteria.andValueIdEqualTo(valueId);
        }
        example.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<Collect> collects = collectMapper.selectByExample(example);
        return BasePageData.ok(collects);
    }

    @Override
    public BasePageData queryFootprint(Integer userId, Integer goodsId, BaseParam param) {
        PageHelper.startPage(param.getPage(), param.getLimit());
        FootprintExample example = new FootprintExample();
        FootprintExample.Criteria criteria = example.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (goodsId != null) {
            criteria.andGoodsIdEqualTo(goodsId);
        }
        example.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<Footprint> footprints = footprintMapper.selectByExample(example);
        return BasePageData.ok(footprints);
    }

    @Override
    public BasePageData queryHistory(Integer userId, String keyword, BaseParam param) {
        PageHelper.startPage(param.getPage(), param.getLimit());
        SearchHistoryExample example = new SearchHistoryExample();
        SearchHistoryExample.Criteria criteria = example.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (keyword != null && !"".equals(keyword)) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        example.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<SearchHistory> histories = searchHistoryMapper.selectByExample(example);
        return BasePageData.ok(histories);
    }

    @Override
    public BasePageData queryFeedback(Integer id, String username, BaseParam param) {
        PageHelper.startPage(param.getPage(), param.getLimit());
        FeedbackExample example = new FeedbackExample();
        FeedbackExample.Criteria criteria = example.createCriteria();
        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        if (username != null && !"".equals(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        example.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<Feedback> feedbacks = feedbackMapper.selectByExample(example);
        return BasePageData.ok(feedbacks);
    }

    @Override
    public User selectUserByUsername(String username) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(username)) {
            criteria.andUsernameEqualTo(username);
        }
        List<User> userList = userMapper.selectByExample(example);
        return userList.size() > 0 ? userList.get(0) : null;
    }

    @Override
    public void updateUserLoginInfo(Integer id, String ip, String sessionId) {
        User user = new User();
        user.setId(id);
        user.setLastLoginIp(ip);
        user.setSessionKey(sessionId);
        user.setUpdateTime(new Date());
        user.setLastLoginTime(new Date());
        userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public void insertUser(RegisterBo registerBo) throws Exception {
        User user = new User();
        String password = registerBo.getPassword();
        password = Md5Utils.getMd5(password, "password");
        user.setPassword(password);
        user.setNickname(registerBo.getUsername());
        user.setUsername(registerBo.getUsername());
        user.setMobile(registerBo.getMobile());
        user.setAddTime(new Date());
        user.setUpdateTime(new Date());
        int insertRows = userMapper.insertSelective(user);
    }

    @Override
    public boolean resetUserPwd(ResetBo resetBo) throws Exception {

        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(resetBo.getMobile())){
            criteria.andMobileEqualTo(resetBo.getMobile());
        }
        List<User> userList = userMapper.selectByExample(example);

        if(userList != null && userList.size() > 0) {
            User user = userList.get(0);
            String password = resetBo.getPassword();
            password = Md5Utils.getMd5(password, "password");
            user.setPassword(password);
            user.setUpdateTime(new Date());
            int update = userMapper.updateByPrimaryKeySelective(user);
            return update != 0;

        }
        return false;
    }

}
